package com.byedu.course;

import com.byedu.base.BaseService;

import com.byedu.domain.course.UserLibraryDO;
import com.byedu.domain.course.UserQuestionErrDO;
import com.byedu.mapper.course.UserLibraryMapper;
import com.byedu.mapper.course.UserQuestionErrMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import java.util.List;

/**
 * Created by jason on 2017/5/21.
 */
@Service
public class UserLibraryService extends BaseService {
    @Autowired
    private UserLibraryMapper userLibraryMapper;

    @Autowired
    private UserQuestionErrMapper userQuestionErrMapper;

    @Transactional
    public Integer addUserLibraryAndErrQuestion(UserLibraryDO userLibraryDO) {
        try {
            //判断错题是否存在
            if (userLibraryDO.getUserQuestionErrDOList() != null && userLibraryDO.getUserQuestionErrDOList().size() != 0) {
                for (UserQuestionErrDO userQuestionErrDO : userLibraryDO.getUserQuestionErrDOList())
                    //新增或修改错题库
                    userQuestionErrMapper.insertOrupdateUserQuestionErr(userQuestionErrDO);
//            userQuestionErrMapper.insertUserQuestionErrBatch(userLibraryDO.getUserQuestionErrDOList());
            }
            //新增userLibrary
            return userLibraryMapper.addUserLibrary(userLibraryDO);
        }catch (Exception ex){
            logger.error(ex.getMessage());
            return 0;
        }
    }

    public List<UserLibraryDO> getUserLibrarybyUserId(String userId) {
            return userLibraryMapper.getUserLibrarybyUserId(userId);
    }

    public List<UserLibraryDO> getUserLibrarysbyLibraryId(int libraryId) {
        return userLibraryMapper.getUserLibrarysbyLibraryId(libraryId);
    }
}
